package com.example.demo.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.pojo.Backgrounduser;
import com.example.demo.pojo.User;
import com.example.demo.service.BackgrounduserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.deploy.net.proxy.pac.PACFunctions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@Api(value = "后台用户",tags = "后台用户")
@Controller
public class backgrounduserController {

    @Autowired
    private BackgrounduserService backgrounduserService;

    //新增后台用户
    @ResponseBody
    @RequestMapping(value = "/insertBackgroundUser",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "新增后台用户",notes = "新增后台用户")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "后台用户账号",name = "accountNumber"),
            @ApiImplicitParam(value = "后台用户密码",name = "password")
    })
    public String insertBackgroundUser(String accountNumber, String password, HttpSession httpSession){
        Backgrounduser backgrounduser1=(Backgrounduser)httpSession.getAttribute("houtaiUser");
        JSONObject jsonObject=new JSONObject();
        if (backgrounduser1.getRole()==1){
            Backgrounduser backgrounduserList=backgrounduserService.selectByName(accountNumber);
            if (backgrounduserList==null){
                Backgrounduser backgrounduser=new Backgrounduser();
                backgrounduser.setAccountnumber(accountNumber);
                backgrounduser.setPassword(password);
                int i=backgrounduserService.insertSelective(backgrounduser);
                if (i>0){
                    jsonObject.put("tishi","添加成功");
                    return jsonObject.toJSONString();
                }else {
                    jsonObject.put("tishi","添加失败");
                    return jsonObject.toJSONString();
                }
            }else {
                jsonObject.put("tishi","用户名以被使用");
                return jsonObject.toJSONString();
            }
        }else {
            return "权限不足";
        }
    }

    //删除后台用户
    @RequestMapping(value = "/delectBackgroundUser",method = RequestMethod.POST)
    @ApiOperation(value = "删除后台用户",notes = "删除后台用户")
    @ApiImplicitParam(value = "后台用户id",name = "backgroundUserId")
    public String delectBackgroundUser(Model model,int backgroundUserId){
        int i=backgrounduserService.deleteByPrimaryKey(backgroundUserId);
        if (i > 0) {
            return "删除成功";
        }
        return "删除失败";
    }

    //查看后台用户
    @RequestMapping(value = "/selectBackroundUser",method = RequestMethod.GET)
    @ApiOperation(value = "查看后台用户",notes = "查看后台用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页数"),
            @ApiImplicitParam(name = "pageSize",value = "页面容量")
    })
    public String selectBackroundUser(Model model,HttpSession httpSession,int pageNum,int pageSize){
        Backgrounduser backgrounduser=(Backgrounduser) httpSession.getAttribute("houtaiUser");
        if (backgrounduser.getRole()==1){
            Page<Backgrounduser> page=backgrounduserService.select(pageNum,pageSize);
            com.github.pagehelper.PageInfo<Backgrounduser> pageInfo=new PageInfo<>(page);
            model.addAttribute("userlists",pageInfo);
            return "user";
        }else {
            return "index";
        }
    }

    //修改后台用户
    @RequestMapping(value = "/updateBackroundUser",method = RequestMethod.POST)
    @ApiOperation(value = "修改后台用户",notes = "修改后台用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "backgroundUserId",value = "后台用户id"),
            @ApiImplicitParam(name = "password",value = "后台用户密码")
    })
    public String updateBackroundUser(int backgroundUserId,String password){
        Backgrounduser backgrounduser=new Backgrounduser();
        backgrounduser.setBackgrounduserid(backgroundUserId);
        backgrounduser.setPassword(password);
        int i=backgrounduserService.updateByPrimaryKeySelective(backgrounduser);
        if (i>0){
            return "修改成功";
        }
        return "修改失败";
    }

    //后台登陆
    @RequestMapping(value = "/backgroundlogin",method = RequestMethod.POST)
    @ApiOperation(value = "后台登陆",notes = "后台登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accountNumber",value = "后台用户账号"),
            @ApiImplicitParam(name = "password",value = "后台用户密码")
    })
    public String login(String accountNumber,String password,HttpSession httpSession,Model model){
        Backgrounduser backgrounduser=backgrounduserService.selectByaccountNumber(accountNumber);
        if (backgrounduser==null){
            model.addAttribute("tishi","不存在该账号");
            return "login";
        }else {
           if (password.equals(backgrounduser.getPassword())){
               httpSession.setAttribute("houtaiUser",backgrounduser);
               model.addAttribute("tishi","登陆成功");
           }else {
               model.addAttribute("tishi","密码错误");
               return "login";
           }
        }
        return "index";
    }

}
